package com.wkbb.basic.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.basic.mapper.DeviceInfoMapper;
import com.wkbb.basic.mapper.VersionControlMapper;
import com.wkbb.basic.mapper.VersionControlRelevanceMapper;
import com.wkbb.basic.model.DeviceInfo;
import com.wkbb.basic.model.VersionControl;
import com.wkbb.basic.model.VersionControlRelevance;
import com.wkbb.basic.service.DeviceVersionControlService;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.dto.basic.DeviceDetailDto;
import com.wkbb.common.dto.basic.OrgHolidayDto;
import com.wkbb.common.dto.basic.VersionControlDto;
import com.wkbb.common.utils.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lizhichao
 * @date 2019/8/22
 * @description
 */
@Service
@Slf4j
public class DeviceVersionControlServiceImpl extends BaseServiceImpl implements DeviceVersionControlService {

    @Autowired
    private VersionControlMapper versionControlMapper;

    @Autowired
    private VersionControlRelevanceMapper versionControlRelevanceMapper;

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Autowired
    public void setMapper(DeviceInfoMapper mapper) {
        super.setMapper(mapper);
    }


    @Override
    public PageInfo<VersionControl> getDeviceVersionPageInfoList(Map<String,Object> parms, Integer pageNo, Integer pageSize) throws Exception {

        //分页
        Page<VersionControl> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<VersionControl> pageInfo = new PageInfo(versionControlMapper.getListByMapParams(parms));
        pageInfo.setTotal(page.getTotal());

        return pageInfo;
    }

    @Override
    public VersionControl getDeviceVersionInfo(Map parms) throws Exception {

        return versionControlMapper.getByMapParams(parms);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDeviceVersion(VersionControlDto versionControlDto) throws Exception {
        VersionControl versionControl = new VersionControl();
        BeanUtil.copyProperties(versionControl ,versionControlDto);
        versionControl.setTarget(versionControlDto.getTargetIdList());
        versionControl.setDeviceType(versionControlDto.getDeviceType());
        versionControlMapper.insert(versionControl);
        List<Long> targetIdList = versionControlDto.getTargetIdList();

        List<VersionControlRelevance> versionControlRelevances = new ArrayList<>();
        if(targetIdList!=null&&!targetIdList.isEmpty()){
            for(Long id : targetIdList){

                if(versionControlDto.getType() == 0){ //0：具体设备
                    VersionControlRelevance versionControlRelevance = new VersionControlRelevance();
                    versionControlRelevance.setVersionId(versionControl.getId());
                    versionControlRelevance.setDeviceId(id);
                    versionControlRelevances.add(versionControlRelevance);
                }

                if(versionControlDto.getType() == 1){ //1：设备类型
                    //查询
                    Map<String,Object> parms = new HashMap();
                    parms.put("deviceTypeId", id);
                    List<DeviceInfo> deviceInfos = deviceInfoMapper.getListByMapParams(parms);

                    if(deviceInfos!=null && !deviceInfos.isEmpty()){
                        for(DeviceInfo deviceInfo : deviceInfos){
                            VersionControlRelevance versionControlRelevance = new VersionControlRelevance();
                            versionControlRelevance.setVersionId(versionControl.getId());
                            versionControlRelevance.setDeviceId(deviceInfo.getId());
                            versionControlRelevances.add(versionControlRelevance);
                        }
                    }
                }

                if(versionControlDto.getType() == 2 && versionControlDto.getDeviceType() !=null &&
                        !StringUtils.isEmpty(versionControlDto.getDeviceType())){ //2：设备所在学校

                    //查询设备类型下的所有设备
                    Map<String,Object> parms = new HashMap();
                    parms.put("list", versionControlDto.getDeviceType());
                    parms.put("orgId", id);
                    List<DeviceInfo> deviceInfos = deviceInfoMapper.getListByDeviceTypeId(parms);

                    if(deviceInfos!=null && !deviceInfos.isEmpty()){
                        for(DeviceInfo deviceInfo : deviceInfos){
                            VersionControlRelevance versionControlRelevance = new VersionControlRelevance();
                            versionControlRelevance.setVersionId(versionControl.getId());
                            versionControlRelevance.setDeviceId(deviceInfo.getId());
                            versionControlRelevances.add(versionControlRelevance);
                         }
                      }

                }
            }

            if(versionControlRelevances!=null && !versionControlRelevances.isEmpty()){
                versionControlRelevanceMapper.insertBatch(versionControlRelevances);
            }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDeviceVersion(VersionControlDto versionControlDto) throws Exception {
        //先删除
        versionControlMapper.deleteByPrimaryKey(versionControlDto.getId());
        versionControlRelevanceMapper.deleteByVersionId(versionControlDto.getId());
        //再增加
        addDeviceVersion(versionControlDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDeviceVersionByIdBatch(List<Long> ids) throws Exception {
        //删除版本控制信息
        versionControlMapper.deleteDeviceVersionByIdBatch(ids);
        //删除关联表信息
        versionControlRelevanceMapper.deleteByVersionIdBatch(ids);
    }

    @Override
    public VersionControlDto getVersionControlDtoByDeviceCode(String deviceCode) throws Exception{
          return versionControlMapper.getVersionControlDtoByDeviceCode(deviceCode);
    }

    @Override
    public void updateStatusById(Map<String,Object> parms) throws Exception{
        versionControlMapper.updateStatusById(parms);
    }

    @Override
    public PageInfo<DeviceDetailDto> getDeviceListByMapParms(Integer pageNo,Integer pageSize,Map<String,Object> parms) throws Exception{

        //分页
        Page<DeviceDetailDto> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<DeviceDetailDto> pageInfo = new PageInfo(versionControlMapper.getDeviceListByMapParms(parms));
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

}
